package org.shj.plugin.tdpl.gen.generator.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.shj.tdpl.common.constant.TdplConstants;

/**
 * @author Shen Huang Jian
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class MultiFileGenerator extends AbstractXlsxGenerator{

	private Logger log = Logger.getLogger(getClass());
	
	@Override
	protected void generate() throws Exception {
		InputStream ins = null;
		XSSFWorkbook oldBook = null;
		XSSFWorkbook book = null;
		
		try{
			Map<String,Integer> oldColNumMap = new HashMap<String,Integer>();// key - table name;
			Map<String,String> staffMap = new LinkedHashMap<String,String>();// key - table name;
			Map<Integer,String> versionTablesMap = new HashMap<Integer,String>();
			
			if(isUpdate){
				ins = new FileInputStream(generatedFilePath + File.separator + "common_tmp.xlsx");
				oldBook = new XSSFWorkbook(ins);
				XSSFSheet oldIndSheet = oldBook.getSheet(TdplConstants.INDEX_SHEET_NAME);
				
				oldColNumMap = getOldSqlColNumMap(oldIndSheet);
				staffMap = getOldStaffSeriesMap(oldIndSheet);
				versionTablesMap = getOldVersionTablesMap(oldBook);
				ins.close();
			}
			
			//create Excel for each table
			for(String tableName : tableMap.keySet()){
				book = new XSSFWorkbook();
				initFontAndStyle(book);
				XSSFSheet sheet = generateNewSheet(book, tableName);
				
				// the first 3 rows are "Column Name", "Data Type" and "Mandatory". So the data is from row 3.
				int lastRow = TdplConstants.FIRST_CONTENT_ROW_IND;
				
				if(isUpdate){
					log.debug("Copy old data in " + tableName + " to new excel.");
					ins = new FileInputStream(generatedFilePath + File.separator + tableName + "_tmp.xlsx");
					oldBook = new XSSFWorkbook(ins);
					
					moveOldValue2NewTable(oldBook,oldColNumMap, sheet, tableMap.get(tableName).size()+2, contentCs);
					XSSFSheet oldSheet = oldBook.getSheet(sheet.getSheetName());
					if(oldSheet != null) {
						//if added one new table
						lastRow = oldSheet.getLastRowNum();
					}
				}
				
				generateSqlCells(sheet,lastRow);
				
				//set freeze pane
				sheet.createFreezePane(1, TdplConstants.FIRST_CONTENT_ROW_IND);
				
				//write to file
				generateFile(book, generatedFilePath, tableName);
			}
			
			
			//create common information Excel which include index sheet and versionTableMap sheet
			book = new XSSFWorkbook();
			XSSFSheet indSheet = book.createSheet(TdplConstants.INDEX_SHEET_NAME);
			
			initFontAndStyle(book);
			fillInfoToIndexSheet(indSheet, staffMap, true);
			
			createVersionTableMapSheet(book, versionTablesMap);
			
			//write to file
			generateFile(book, generatedFilePath, TdplConstants.Common_Info_File_Name);
		    
		}catch(Exception e){
			log.error(e);
			throw e;
		}finally{
			if(ins != null){
				ins.close();
			}
		}
	}
	
	
	
	
	
		

}
